package com.baiyang.app.ui.register;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.HomeActivity;
import com.baiyang.app.MyApp;
import com.baiyang.app.R;
import com.baiyang.app.databinding.ActivityRegisterBinding;
import com.baiyang.app.model.User;
import com.baiyang.app.ui.login.LoginActivity;

public class RegisterActivity extends AppCompatActivity {

    private ActivityRegisterBinding activityRegisterBinding;
    private RegisterService service;
    private RegisterViewModel viewModel;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityRegisterBinding = DataBindingUtil.setContentView(this,R.layout.activity_register);
        user = new User();
        activityRegisterBinding.setUser(user);
        viewModel = new ViewModelProvider(this).get(RegisterViewModel.class);
        service = new RegisterService(getApplicationContext(),viewModel);
        init();
        initHandler();
    }

    private void initHandler() {
        viewModel.getRegisterResult().observe(this, new Observer<JSONObject>() {
            @Override
            public void onChanged(JSONObject jsonObject) {
                String result = jsonObject.getString("result");
                String detail = jsonObject.getString("detail");
                if("SUCCESS".equals(result)){
                    User user = jsonObject.getObject("data",User.class);
                    // 将user存储到myapp
                    MyApp.getInstance().setUser(user);
                    Intent intent = new Intent();
                    intent.setClass(getApplicationContext(), HomeActivity.class);
                    startActivity(intent);
                    finish();
                    overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
                }else{
                    Toast.makeText(getApplicationContext(),detail,Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void init(){
        activityRegisterBinding.toolbar.setNavigationIcon(R.drawable.ic_back);
        activityRegisterBinding.toolbar.setNavigationOnClickListener(e->{
            Intent intent = new Intent();
            intent.setClass(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            finish();
            overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
        });
        activityRegisterBinding.register.setOnClickListener(e->{
            if(R.id.man == activityRegisterBinding.sex.getCheckedRadioButtonId()){
                user.setSex("1");
            }else{
                user.setSex("0");
            }
            service.register(user);
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.activityRegisterBinding = null;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
    }
}